package models;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import entities.OwnerDomain;

public class OwnerDomainModel {
    private Objectify ofy;
    
    private Cache cache = null;
    
    private String prefix = "owner_";
    
    private static OwnerDomainModel instance = null;
	
	public static OwnerDomainModel getInstance() {
		if (instance == null) {
			instance = new OwnerDomainModel();
		}
		
		return instance;
	}

    public OwnerDomainModel() {
        try {
            ObjectifyService.register(OwnerDomain.class);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        
        try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           //e.printStackTrace();
        }
        
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
        ofy = ObjectifyService.begin(opts);
    }
    
    public ArrayList<OwnerDomain> getVerifyDomainChange(int page, int limit) {
        ArrayList<OwnerDomain> domains = new ArrayList<OwnerDomain>();
        
        Calendar cal=Calendar.getInstance();
		Date now = new Date();
		cal.setTime(now);
		cal.add(Calendar.DATE, -3);
        
        int start = (page - 1) * limit;
        Query<OwnerDomain> q = ofy.query(OwnerDomain.class).filter("isUpdated =", 0).filter("lastUpdate <=", cal.getTime()).limit(limit).offset(start);
        for (OwnerDomain domain: q) {
            domains.add(domain);
        }
        
        return domains;
    }
    
    public ArrayList<OwnerDomain> getVertifyOwnerDomain(String username) {
        ArrayList<OwnerDomain> domains = new ArrayList<OwnerDomain>();
        
        Query<OwnerDomain> q = ofy.query(OwnerDomain.class).filter("username", username).filter("status", 1);
        for (OwnerDomain domain: q) {
            domains.add(domain);
        }
        
        return domains;
    }
    
    public ArrayList<OwnerDomain> getUnvertifyOwnerDomain(String username) {
        ArrayList<OwnerDomain> domains = new ArrayList<OwnerDomain>();
        
        Query<OwnerDomain> q = ofy.query(OwnerDomain.class).filter("username", username).filter("status", 0);
        for (OwnerDomain domain: q) {
            domains.add(domain);
        }
        
        return domains;
    }
    
    public OwnerDomain getOwnerDomain(String id) {
        OwnerDomain ownerDomain = new OwnerDomain();
        
        if (cache != null && cache.containsKey(prefix + id)) {
            ownerDomain = (OwnerDomain) cache.get(prefix + id);
        } else {    
            try {
                ownerDomain = ofy.get(new Key<OwnerDomain>(OwnerDomain.class, id));
                if (cache != null) {
                	cache.put(prefix + id, ownerDomain);
                }
            } catch (Exception e) {
                ownerDomain = null;
            }
        }
        return ownerDomain;
    }
    
    public OwnerDomain getByDomain(String domainName) {
        OwnerDomain ownerDomain = new OwnerDomain();
        if (cache != null && cache.containsKey(prefix + domainName)) {
            ownerDomain = (OwnerDomain) cache.get(prefix + domainName);
        } else {
            try {
                ownerDomain = ofy.query(OwnerDomain.class).filter("domain", domainName).get();
                if (cache != null) {
                	cache.put(prefix + domainName, ownerDomain);
                }
            } catch (Exception e) {
                ownerDomain = null;
            }
        }
        return ownerDomain;
    }
    
    public boolean checkExists(String id) {
        boolean exists = false;
        OwnerDomain ownerModel = getOwnerDomain(id);
        if (ownerModel != null) {
            exists = true;
        }
        
        return exists;
    }
    
    public void insert(OwnerDomain ownerDomain) {
        if (cache != null) {
            cache.put(prefix + ownerDomain.getId(), ownerDomain);
            cache.put(prefix + ownerDomain.getDomain(), ownerDomain);
        }
        
        ofy.put(ownerDomain);
    }
    
    public void delete(OwnerDomain ownerDomain) {
        if (cache != null && cache.containsKey(prefix + ownerDomain.getId())) {
            cache.remove(prefix + ownerDomain.getId());
        }
        
        if (cache != null && cache.containsKey(prefix + ownerDomain.getDomain())) {
            cache.remove(prefix + ownerDomain.getDomain());
        }
        ofy.delete(ownerDomain);
    }
}
